package example.tallydemo.MyTools;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by tuzha on 2018/4/15.
 */

public class MyTimeTool
{
    int year;
    int month;
    int day;

    public MyTimeTool(Date date)
    {
        setDate(date);
    }

    public MyTimeTool(){}

    public void setDate(Date date)
    {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String str = df.format(date);
        String str_date[] = str.split("-");
        this.year = Integer.valueOf(str_date[0]);
        this.month = Integer.valueOf(str_date[1]);
        this.day = Integer.valueOf(str_date[2]);
    }

    public void setDate(String str)
    {
        String str_date[] = str.split("-");
        this.year = Integer.valueOf(str_date[0]);
        this.month = Integer.valueOf(str_date[1]);
        this.day = Integer.valueOf(str_date[2]);
    }

    public String DateToString(Date date)
    {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String str = df.format(date);
        return str;
    }

    public String setDateToString(int year,int month,int day)
    {
        return DateToString(setDateStart(year,month,day));
    }

    public Date StringToDate(String str)
    {
        String str_date[] = str.split("-");
        this.year = Integer.valueOf(str_date[0]);
        this.month = Integer.valueOf(str_date[1]);
        this.day = Integer.valueOf(str_date[2]);
        return setDateStart(year,month,day);
    }

    public Date setDateStart(int year,int month,int day)
    {
        Calendar c = Calendar.getInstance();
        c.set(year,month - 1,day);
        Date date = c.getTime();
        return date;
    }


    public Date getFirstDate(Date date)
    {
        setDate(date);
        return setDateStart(year,month,1);
    }

    public void setTody()
    {
        setDate(getToday());
    }

    public Date getFirstDate()
    {
        setDate(getToday());
        return setDateStart(year,month,1);
    }

    public Date getToday()
    {
        Date date =  new Date();
        return date;
    }

    public int getYear()
    {
        return this.year;
    }

    public int getMonth()
    {
        return this.month;
    }

    public int getDay()
    {
        return this.day;
    }
}
